package com.tanhua.recommend.message;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.recommend.pojo.RecommendMoment;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @ClassName :  MomentMessageConsumer
 * @Author : Tenebrous
 * @Date: 2020/12/18 18:50
 * @Description :
 */
@Component
@RocketMQMessageListener(topic = "tanhua-quanzi",consumerGroup = "tanhua-quanzi-consumer")
public class MomentMessageConsumer implements RocketMQListener<String> {

    private static final ObjectMapper MAPPER = new ObjectMapper();

    private static final Logger LOGGER = LoggerFactory.getLogger(MomentMessageConsumer.class);

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String message) {
        try {
            JsonNode jsonNode = MAPPER.readTree(message);
            int type = jsonNode.get("type").asInt();
            String publishId = jsonNode.get("publishId").asText();
            Long date = jsonNode.get("date").asLong();
            Long userId = jsonNode.get("userId").asLong();
            Long pid = jsonNode.get("pid").asLong();

            RecommendMoment recommendMoment = new RecommendMoment();
            recommendMoment.setPublishId(pid);
            recommendMoment.setDate(date);
            recommendMoment.setId(ObjectId.get());
            recommendMoment.setUserId(userId);

            switch (type) {
                case 1: {
                    int score = 0;
                    Publish publish = this.mongoTemplate.findById(new ObjectId(publishId), Publish.class);
                    if (StringUtils.length(publish.getText()) < 50) {
                        score += 1;
                    } else if (StringUtils.length(publish.getText()) < 100) {
                        score += 2;
                    } else if (StringUtils.length(publish.getText()) >= 100) {
                        score += 3;
                    }

                    if (!CollectionUtils.isEmpty(publish.getMedias())) {
                        score += publish.getMedias().size();
                    }

                    recommendMoment.setScore((double) score);

                    break;
                }
                case 2: {
                    recommendMoment.setScore(1d);
                    break;
                }
                case 3: {
                    recommendMoment.setScore(5d);
                    break;
                }
                case 4: {
                    recommendMoment.setScore(8d);
                    break;
                }
                case 5: {
                    recommendMoment.setScore(10d);
                    break;
                }
                case 6: {
                    recommendMoment.setScore(-5d);
                    break;
                }
                case 7: {
                    recommendMoment.setScore(-8d);
                    break;
                }
                default: {
                    recommendMoment.setScore(0d);
                    break;
                }
            }

            String collectionName = "recommend_quanzi_" + new DateTime().toString("yyyyMMdd");
            this.mongoTemplate.save(recommendMoment, collectionName);

        } catch (IOException e) {
            LOGGER.error("消息处理失败！message = " + message);
        }
    }
}
